package com.hkyl.hospital.web.controller;

import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.domain.ResultVo;
import com.hkyl.hospital.entity.Hospitalized;
import com.hkyl.hospital.service.HospitalizedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author sjh
 * @date 2023/07/13 16:40:25
 */
@RestController
@RequestMapping("hospitalized")
public class HospitalizedController {

    Logger logger = LoggerFactory.getLogger(HospitalApplysController.class);

    @Autowired
    private HospitalizedService hospitalizedService;

    @PostMapping("delete")
    public ResultVo delete(@RequestBody Map map){
        Integer hospitalizedId = (Integer) map.get("hospitalizedId");
        int rows = hospitalizedService.delete(hospitalizedId);
        return rows > 0 ? ResultVo.success() : ResultVo.failure("");
    }

    @PostMapping("update")
    public ResultVo update(@RequestBody Hospitalized hospitalized){
        logger.info(String.valueOf(hospitalized));
        int rows = hospitalizedService.update(hospitalized);
        return rows > 0 ? ResultVo.success() : ResultVo.failure("");
    }

    @PostMapping("select")
    public ResultVo selectAll(@RequestBody Map map){
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<Hospitalized> hospitalizeds = new PageInfo<>(hospitalizedService.selectAll(pageNumber, pageSize));
        return ResultVo.success(hospitalizeds);
    }

    @PostMapping("selectIn")
    public ResultVo selectIn(@RequestBody Map map){
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<Hospitalized> hospitalizeds = new PageInfo<>(hospitalizedService.selectIn(pageNumber, pageSize));
        return ResultVo.success(hospitalizeds);
    }

    @PostMapping("selectOut")
    public ResultVo selectOut(@RequestBody Map map){
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<Hospitalized> hospitalizeds = new PageInfo<>(hospitalizedService.selectOut(pageNumber, pageSize));
        return ResultVo.success(hospitalizeds);
    }

    @PostMapping("insert")
    public ResultVo insert(@RequestBody Hospitalized hospitalized){
        logger.info(String.valueOf(hospitalized));
        int rows = hospitalizedService.insert(hospitalized);
        return ResultVo.success();
    }

    @PostMapping("selectByName")
    public ResultVo selectByName(@RequestBody Map map){
        String patientName = (String) map.get("patientName");
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<Hospitalized> hospitalizedPageInfo = new PageInfo<>(hospitalizedService.selectByName(patientName, pageNumber, pageSize));
        return ResultVo.success(hospitalizedPageInfo);
    }

    @PostMapping("surePatientInHospitalized")
    public ResultVo surePatientInHospitalized(@RequestBody String patientId) {
        if (!StringUtils.hasText(patientId)){
            throw new RuntimeException("病人id为空");
        }
        Hospitalized hospitalized = hospitalizedService.findByPatientId(Integer.valueOf(patientId));
        return hospitalized == null ? ResultVo.failure("") : ResultVo.success("同意", hospitalized);
    }

    @PostMapping("updateReaded")
    public ResultVo updateReaded(@RequestBody Hospitalized hospitalized) {

        return ResultVo.success(hospitalizedService.updateReaded(hospitalized));
    }
}
